package com.z.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.z.common.web.vo.request.PageVo;
import com.z.sys.entity.BackConfig;
import com.z.sys.mapper.BackConfigMapper;
import com.z.sys.service.IBackConfigService;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 备份配置Service业务层处理
 *
 * @author z
 * @date 2023-12-27
 */
@Service
public class BackConfigServiceImpl extends ServiceImpl<BackConfigMapper,BackConfig> implements IBackConfigService {


    /**
     * 查询备份配置
     * @param backConfig 备份配置
     * @param pageVo
     * @return 备份配置 分页集合
     * */
    @Override
    public PageInfo<BackConfig> selectBackConfigPage(BackConfig backConfig, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());
        List<BackConfig> data = selectBackConfigList(backConfig);
        return new PageInfo<>(data);
    }

    @Override
    public List<BackConfig> selectBackConfigList(BackConfig backConfig) {
        List<BackConfig> data = baseMapper.selectBackConfigList(backConfig);
        return data;
    }

    @Override
    public void backDir(String sourceDir, String targetDir) {
        // 指定要压缩的目录
        File sourceDirectory = new File(sourceDir);

        // 指定要将压缩文件保存到的目录
        File targetDirectory = new File(targetDir);

        // 创建目标目录（如果不存在）
        targetDirectory.mkdirs();

        // 创建压缩文件
        File zipFile = new File(targetDirectory, sourceDirectory.getName()+".zip");

        // 创建ZipOutputStream
        FileOutputStream fos = null;
        ZipOutputStream zipOut = null;
        try {
            fos = new FileOutputStream(zipFile);
            zipOut = new ZipOutputStream(fos);

                // 递归压缩目录
            zipDirectory(sourceDirectory, sourceDirectory.getName(), zipOut);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(zipOut != null){
                try {
                    zipOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    private static void zipDirectory(File directory, String parent, ZipOutputStream zipOut) throws IOException {
        File[] files = directory.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    zipDirectory(file, parent + "/" + file.getName(), zipOut);
                } else {
                    FileInputStream fis = new FileInputStream(file);

                    // 创建ZipEntry，并设置文件名
                    ZipEntry zipEntry = new ZipEntry(parent + "/" + file.getName());

                    // 将ZipEntry添加到ZipOutputStream
                    zipOut.putNextEntry(zipEntry);

                    // 写入文件内容到ZipOutputStream
                    byte[] bytes = new byte[1024];
                    int length;
                    while ((length = fis.read(bytes)) >= 0) {
                        zipOut.write(bytes, 0, length);
                    }

                    // 关闭ZipEntry
                    zipOut.closeEntry();
                    // 关闭文件输入流
                    fis.close();
                }
            }
        }
    }
}
